package cn.com.yang.socket;

/**
 * 计算经纬度
 * TODO
 * @author yangyijin
 * @since 2024年5月31日-下午3:14:04
 */
public class CalXYEarth {
	
	private static double R=6371.004;
	
	/**
	 * 如何推导的？？
	 * R = 6371.004km
	 * D = R * arccos(siny1*siny2 + cosy1cosy2*cos(x1-x2))
	 * x1和y1是点A的经纬度，x2和y2是点B的经纬度。
	 * 东经为正，西经为负；北纬为正，南纬为负
	 * @param lat1 维度1
	 * @param lon1 经度1
	 * @param lat2 维度2
	 * @param lon2 经度2
	 * @return
	 */
	public static double getTwoPointDistance(double lon1,double lat1,double lon2,double lat2) {
		double dLat = Math.toRadians(lat2 - lat1);  
        double dLon = Math.toRadians(lon2 - lon1);  
  
        lat1 = Math.toRadians(lat1);  
        lat2 = Math.toRadians(lat2);  
  
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)  
                + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);  
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));  
  
        double distance = R * c;  
  
        return distance;
	}
	public static void main(String[] args) {
		double distance = CalXYEarth.getTwoPointDistance(116.123, 34.23, 113.8, 37.22);
		System.out.print(distance);
	}
}
